package com.example.pufang.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.common.CommonResult;
import com.example.common.PageParamRequest;
import com.example.pufang.pojo.SystemMessage;
import com.example.pufang.service.AdminService;
import com.example.pufang.service.SystemMessageService;
import com.example.pufang.vo.PageListVo;
import com.example.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/systemMessage")
@CrossOrigin
@Api(tags = "web系统消息接口")
public class SystemMessageController {

    @Autowired
    private SystemMessageService systemMessageService;

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private AdminService adminService;


    @Operation(summary = "获取分页用户列表")
    @GetMapping("/getListByPage")
    public CommonResult<PageListVo> getListByPage(PageParamRequest pageParamRequest, String searchText) {
        return CommonResult.success(systemMessageService.getListByPage(pageParamRequest, searchText));
    }

    @Operation(summary = "新增系统消息")
    @PostMapping("/save")
    public CommonResult<Boolean> saveSystemMessage(@RequestBody SystemMessage systemMessage, HttpServletRequest request) {

        Integer id = tokenUtil.getAdminId(request);
        if (ObjectUtils.isNull(id)) {
            return CommonResult.failed("获取当前用户失败");
        } else {
            systemMessage.setAdminId(id);
        }
        if (systemMessageService.save(systemMessage)) {
            return CommonResult.success("保存成功");
        } else {
            return CommonResult.success("保存失败");
        }
    }

    @Operation(summary = "修改系统消息")
    @PostMapping("/update")
    public CommonResult<Boolean> updateSystemMessage(@RequestBody SystemMessage systemMessage) {
        if (systemMessageService.updateById(systemMessage)) {
            return CommonResult.success("保存成功");
        } else {
            return CommonResult.success("保存失败");
        }
    }


    @Operation(summary = "删除系统雄安锡")
    @PostMapping("/delete")
    public CommonResult<Boolean> deleteSystemMessage(@RequestBody SystemMessage systemMessage) {
        if (systemMessageService.removeById(systemMessage)) {
            return CommonResult.success("删除成功");
        } else {
            return CommonResult.success("删除失败");
        }
    }


}
